/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.LoggedData.panels;

import com.toedter.calendar.JDateChooser;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import researchgrants.parts.date.DateForDb;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsDate extends LoggedDataPanels {

    
    
    @Override
    public Component getViewComponent() {
        DateForDb date=null;
        JLabel lblViewData=new JLabel();
        if (!getData().value().equals("")) {
            try {
                date = DateForDb.fromDbString(getData().value());
            } catch (ParseException ex) {
                Logger.getLogger(LoggedDataPanelsDate.class.getName()).log(Level.SEVERE, null, ex);
            }
            lblViewData.setText(date.toDisplayString());
        }
        return(lblViewData);
    }

    JDateChooser dateChooser=new JDateChooser();
    
    // Because the Date Listener is currently uses a task to get the value,
    // it is vital that all references to it be synchronized.
    
    @Override
    synchronized public Component getEditComponent() {
        dateChooser.addPropertyChangeListener("date", new DateChooserListener());
        if (hasDataInitialized()) {
            if (!getData().value().equals("")) {
                try {
                    DateForDb date = DateForDb.fromDbString(getData().value());
                    dateChooser.setDate(date.toDate());
                } catch (ParseException ex) {
                    Logger.getLogger(LoggedDataPanelsDate.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            String defaultValue = getDefaultValue();
            if (!defaultValue.equals("")) {
                try {
                    dateChooser.setDate(DateForDb.fromDbString(defaultValue).toDate());
                } catch (ParseException ex) {
                    Logger.getLogger(LoggedDataPanelsDate.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return(dateChooser);
    }

    @Override
    synchronized public String getEdittedValue() {
        Date newDate=dateChooser.getDate();
        if (newDate==null) {
            return("");
        } else {
            DateForDb date=DateForDb.fromDate(newDate);
            return(date.toDbString());
        }
    }

    @Override
    synchronized public boolean isEmpty() {
        return(dateChooser.getDate()==null);
    }

    private class DateChooserListener implements PropertyChangeListener {

        // To alleviate the non-interactiveness of the JDateChooser object, 
        // I manually raise a change every few milliseconds
        public DateChooserListener() {
            TimerTask task=new TimerTask() {

                @Override
                public void run() {
                    propertyChange(new PropertyChangeEvent(this, "date", null, null));
                }
            };
            Timer timer=new Timer();
            timer.scheduleAtFixedRate(task, 0, 100);
        }

        public void propertyChange(PropertyChangeEvent evt) {
            notifyAllChangeListeners(new ChangeEvent(evt.getSource()));
        }

    }

    @Override
    public boolean isValid() {
        return(true);
    }
    
}
